﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;

namespace Black.Minecraft.Editor.Models
{
    [Serializable()]
    [XmlRoot(ElementName = "Enchant")]
    public class Enchant
    {
        #region Propriétés

        /// <summary>
        /// Obtient ou définit l'identifiant
        /// </summary>
        [XmlElement(typeof(short), ElementName = "Id")]
        public short Id
        {
            get;
            set;
        }

        /// <summary>
        /// Obtient ou définit le libellé
        /// </summary>
        [XmlElement(typeof(string), ElementName = "Label")]
        public string Label
        {
            get;
            set;
        }

        /// <summary>
        /// Obtient ou définit le niveau maximum
        /// </summary>
        [XmlElement(typeof(short), ElementName = "MaxLevel")]
        public short MaxLevel
        {
            get;
            set;
        }

        /// <summary>
        /// Obtient ou définit la liste des items concernés par cet enchantement
        /// </summary>
        [XmlArray(ElementName = "EnchantTargets")]
        [XmlArrayItem(ElementName = "EnchantTarget", Type = typeof(short))]
        public List<short> EnchantTargets
        {
            get;
            set;
        }

        #endregion

        #region Constructeurs

        /// <summary>
        /// Constructeur de la classe
        /// </summary>
        public Enchant()
        {
            this.EnchantTargets = new List<short>();
        }

        #endregion

        #region Fonctions statiques

        /// <summary>
        /// Charge la liste des enchantemetns depuis la ressource 
        /// </summary>
        /// <returns>Liste des objets</returns>
        public static List<Enchant> LoadEnchants()
        {
            try
            {
                List<Enchant> list;
                Assembly assembly = Assembly.GetExecutingAssembly();

                using (Stream stream = assembly.GetManifestResourceStream("Black.Minecraft.Editor.Assets.EnchantsList.xml"))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<Enchant>), new XmlRootAttribute("Root"));
                    list = (List<Enchant>)serializer.Deserialize(stream);
                }

                return (list);
            }
            catch (Exception e)
            {
                Debug.WriteLine("Error : " + e.Message);
                return (null);
            }
        }

        #endregion
    }
}
